<?php

namespace app\storeapi\controller\apps\sharp;

use app\storeapi\model\Category as CategoryModel;
use app\storeapi\controller\Controller;
use app\storeapi\model\Goods as GoodsModel;
use app\storeapi\service\Goods as GoodsService;
use app\storeapi\model\sharp\Goods as SharpGoodsModel;
/**
 * 秒杀商品管理
 * Class Goods
 * @package app\store\controller\apps\sharp
 */
class Goods extends Controller
{

    /**
     * 获取商品列表--未添加的秒杀商品
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function goodsLists()
    {
        $param = $this->postData();
        // 获取全部商品列表
        $model = new GoodsModel;
        // 商品分类
        $catgory = CategoryModel::getCacheTree();
        //查找已经添加的秒杀商品
        $sharpGoodsIds = (new SharpGoodsModel)->sharpGoodsIds();
        //获取未添加秒杀的商品
        $param['sharpGoodsIds'] = $sharpGoodsIds;
        $param['spec_type'] = 10;
        // 商品列表
        $list = $model->getGoodsList($param);
        return $this->renderSuccess(compact('list','catgory'));
    }

    /**
     * 秒杀商品列表--已经添加的秒杀商品
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sharpGoods()
    {
        $param = $this->postData();
        $model = new SharpGoodsModel;
        $list = $model->getSharpList($param);
        // 商品分类
        $catgory = CategoryModel::getCacheTree();
        return $this->renderSuccess(compact('list','catgory'));
    }

    /**
     * 添加秒杀商品获取商品详情
     * @param int $step
     * @param null $goods_id
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function detail()
    {
        $param = $this->postData();
        $model = new SharpGoodsModel;
        //验证商品ID能否被添加
        if(!$model->validateGoodsId($param['goods_id'])){
            return $this->renderError($model->getError() ?: '添加失败');
        }
        // 商品信息
        $goods = (new GoodsModel)->detail($param);
        $specData = GoodsService::getSpecData($goods);
        return $this->renderSuccess(compact('goods', 'specData'));

    }


    /**
     * 添加秒杀商品
     * @param $goodsId
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $getparam = $this->postData();
        $param['goods_id'] = $getparam['goods_id'];
        $param['wxapp_id'] = $getparam['wxapp_id'];
        //新增秒杀商品
        $data = $getparam['goods'];
        $model = new SharpGoodsModel;
        // 验证商品ID能否被添加
        if (!$model->validateGoodsId($getparam['goods_id'])) {
            return $this->renderError($model->getError() ?: '添加失败');
        }
        // 商品信息
        $goods = (new GoodsModel)->detail($param);
        // 表单提交
        if ($model->add($goods, $data)) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');

    }

    /**
     * 编辑秒杀商品获取详情
     * @param $sharp_goods_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function sharpdetail()
    {
        $param = $this->postData();
        $sharp_goods_id = $param['sharp_goods_id'];
        // 秒杀商品详情
        $model = SharpGoodsModel::detail($sharp_goods_id, ['sku']);
        $getgoods['goods_id'] = $model['goods_id'];
        $getgoods['wxapp_id'] = $param['wxapp_id'];
        // 商品信息
        $goods = (new GoodsModel)->detail($getgoods);
        // 商品多规格信息
        $specData = $model->getSpecData($goods, $model['sku']);
        return $this->renderSuccess(compact('model', 'goods', 'specData'));
        
    }


    /**
     * 编辑秒杀商品
     * @param $sharp_goods_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function edit()
    {
        $param = $this->postData();
        $sharp_goods_id = $param['sharp_goods_id'];
        // 秒杀商品详情
        $model = SharpGoodsModel::detail($sharp_goods_id, ['sku']);
        // 秒杀商品详情
        $getgoods['goods_id'] = $model['goods_id'];
        $getgoods['wxapp_id'] = $param['wxapp_id'];
        // 商品信息
        $goods = (new GoodsModel)->detail($getgoods);
        $data =  $param['goods'];
        // 表单提交
        if ($model->edit($goods, $data)) {
            return $this->renderSuccess([],'更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');

    }


    /**
     * 删除秒杀商品
     * @param $sharp_goods_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        $sharp_goods_id = $param['sharp_goods_id'];
        // 秒杀商品详情
        $model = SharpGoodsModel::detail($sharp_goods_id);
        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}